{% extends "stream/base.html" %}

{% block title %}
Add new content
{% endblock %}

{% block scripts %}
    <script type="text/javascript" src="/js/jquery.form.js"></script>
    <script type="text/javascript">
        function GetURLParameters() {
            var base_regex_string = '(\\w+)=([^&#]*)';
            var regex_string = '[?&]' + base_regex_string;
            var global_regex = new RegExp(regex_string, 'g');
            var matches = window.location.href.match(global_regex);
            if (matches == null) {
                return {};
            }
            else {
                var local_regex = new RegExp(base_regex_string);
                var results = {};
                for (var i = 0; i < matches.length; ++i) {
                    var match = matches[i].match(local_regex);
                    results[match[1]] = decodeURIComponent(match[2]);
                }
                return results;
            }
        }

        function FillForm() {
            var url_parameters = GetURLParameters();
            for (item in url_parameters) {
                $('#id_' + item).attr('value', url_parameters[item]);
            }

            function SetButtons() {
                function CloseOrNext() {
                    var jump_param_string = 'save';
                    if (jump_param_string in url_parameters && url_parameters[jump_param_string] == 'close') {
                        window.close();
                    } else {
                        location.href = '{% url stream.views.stream viewer.id %}'
                    }
                }

                $('#save').bind('click', function(e) {
                    form = $(e.target.form);
                    form.ajaxSubmit({
                        success : function(data) {
                            if (data.status == 'success') {
                                alert('success');
                                CloseOrNext();
                            } else if (data.status == 'error') {
                                alert('The server encountered an error while processing your request');
                            } else {
                                $('#form-div').html('' + data);
                                SetButtons();
                            }
                        },
                        dataType: 'json'
                    });
                    /*$.post(form.attr('action'), form.serialize(), function(data) {

                    }, 'json');*/
                });
                $('#close').bind('click', CloseOrNext);
            }

            SetButtons();
        }

        function Init() {
            {% if form %}
            FillForm();
            {% else %}
            $('#type-select').bind('change', function(e) {
                selected_value = e.target.options[e.target.selectedIndex].value;
                if (selected_value != '') {
                    $.get('{% url stream.views.addForm %}', { 'form_name' : e.target.options[e.target.selectedIndex].value }, function(data) {
                        $('#form-div').html(data);
                        FillForm();
                    })
                } else {
                    $('#form-div').empty();
                }
            })
            {% endif %}
        }

        $(document).ready(Init);
    </script>
{% endblock %}

{% block content %}

{% if form %}
    <div id="form-div">
    {% include "stream/addForm.html" %}
    </div>
{% else %}
Add new <select id="type-select" name="type-select">
    <option value=''>---------------</option>
    <option value="bookmark_form">Bookmark</option>
    <option value="feed_form">News feed</option>
    <option value="delicious_form">Delicious bookmarks</option>
    <option value="twitter_form">Twitter stream</option>
    <option value="opml_form">OPML file</option>
</select>

<div id="form-div" />
{% endif %}
{% endblock %}
